package com.talent.web;

import com.talent.common.Const;
import com.talent.page.Page;
import com.talent.pojo.Depart;
import com.talent.pojo.Major;
import com.talent.pojo.User;
import com.talent.service.DepartService;
import com.talent.service.MajorService;
import com.talent.service.UserService;
import com.talent.vo.Result;
import com.talent.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/manage/user/")
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @Autowired
    DepartService departService;

    @Autowired
    MajorService majorService;

    @RequestMapping("/manager")
    public ModelAndView manager(ModelAndView mav, UserVo userVo, @RequestParam("type") Integer type, Integer currentPage){
        Page<UserVo> page = userService.getUserVoPage(userVo, type, currentPage == null ? 1: currentPage);
        initPageParams(mav, page);
        mav.addObject("page", page.getResultList());
        if(type == Const.USER_TYPE.USER_TYPE_ADMIN.getCode()){
            mav.setViewName("/user/admin/manager");
        }else if(type == Const.USER_TYPE.USER_TYPE_DEPART.getCode()){
            mav.setViewName("/user/depart/manager");
        }else{
            mav.setViewName("/user/major/manager");
        }
        mav.addObject("userVo", userVo);
        return mav;
    }


    /**
     * 批量删除
     * @return
     */
    @RequestMapping(value = "userDeleteIDs", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteIDs(String ids) {
        try {
            userService.deleteAll(ids.split(","));
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }


    @RequestMapping(value = "validate", method = RequestMethod.POST)
    @ResponseBody
    public Object validate(String code) {
        try {
            int count = userService.selectCount(code.trim());
            if(count>0){
                return renderError("已存在");
            }
            return renderSuccess("");
        } catch (Exception e) {
            return renderError("班级编号出错");
        }

    }

    /**
     * @Title: addUI
     * @Description: 去添加页面
     * @return
     */

    @RequestMapping("addUI")
    public ModelAndView courseAddUI(@RequestParam("type") Integer type) {
        ModelAndView mav = new ModelAndView();
        try {
            if(type == Const.USER_TYPE.USER_TYPE_ADMIN.getCode()){
                mav.setViewName("/user/admin/addUI");
            }else if(type == Const.USER_TYPE.USER_TYPE_DEPART.getCode()){
                List<Depart> list = departService.selectAllDepart();
                mav.addObject("list", list);
                mav.setViewName("/user/depart/addUI");
            }else{
                List<Major> list = majorService.selectAllMajor();
                mav.addObject("list", list);
                List<Depart> departList = departService.selectAllDepart();
                mav.addObject("departList", departList);
                mav.setViewName("/user/major/addUI");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.setViewName("common/error");
            mav.addObject("error", "有异常！");
        }
        return mav;
    }

    @RequestMapping("add")
    @ResponseBody
    public Result courseAdd(UserVo userVo) {
        try {
            userService.addUserVo(userVo);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "userDelete",method = RequestMethod.POST)
    @ResponseBody
    public Result classDelete(Integer id) {
        try {
            userService.deleteById(id);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }


    /**
     * 编辑
     * @param id
     * @return
     */
    @RequestMapping("userUpdateUI")
    public ModelAndView userUpdateUI(Integer id, @RequestParam("type") Integer type) {
        ModelAndView mav = new ModelAndView();
        try {
            UserVo userVo = userService.selectById(id);
            mav.addObject("userVo", userVo);
            if(type == Const.USER_TYPE.USER_TYPE_ADMIN.getCode()){
                mav.setViewName("/user/admin/editUI");
            }else if(type == Const.USER_TYPE.USER_TYPE_DEPART.getCode()){
                List<Depart> list = departService.selectAllDepart();
                mav.addObject("list", list);
                mav.setViewName("/user/depart/editUI");
            }else{
                List<Major> list = majorService.selectAllMajor();
                mav.addObject("list", list);
                mav.setViewName("/user/major/editUI");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.setViewName("common/error");
            mav.addObject("error", "有异常！");
        }
        return mav;
    }

    @RequestMapping("userUpdate")
    @ResponseBody
    public Result classUpdate(UserVo userVo) {

        try {
            userService.editUserVo(userVo);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    @RequestMapping("/userInfo/{id}")
    public ModelAndView getUserInfo(@PathVariable Integer id, ModelAndView mav){

        UserVo userVo = userService.getUserVoById(id);
        mav.addObject("user", userVo);
        mav.setViewName("/userInfo");
        return mav;
    }


    @RequestMapping("updatePasswordUI")
    public ModelAndView updatePasswordUI(Integer id) {
        ModelAndView mav = new ModelAndView();
        try {
            UserVo userVo = userService.selectById(id);
            mav.addObject("userVo", userVo);
            mav.setViewName("/user/editPassword");
        } catch (Exception e) {
            e.printStackTrace();
            mav.setViewName("common/error");
            mav.addObject("error", "有异常！");
        }
        return mav;
    }

    @RequestMapping("updatePassword")
    @ResponseBody
    public Result updatePassword(@RequestParam("oldPassword") String oldPassword, @RequestParam("password") String password) {

        try {
            User user = (User) getSession().getAttribute(Const.CURRENT_USER);
            if (user == null) {
                return new Result(false, "用户未登录");
            }
            userService.editPassword(oldPassword, password, user);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    @RequestMapping("validatePassword")
    @ResponseBody
    public int validatePassword(String password) {
        User user = (User) getSession().getAttribute(Const.CURRENT_USER);
        return userService.validate(password,user);
    }


    @RequestMapping("resetPassword")
    @ResponseBody
    public Result resetPassword(Integer id) {
        try {
            userService.resetPassword(id);
            return new Result(true);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false);
        }
    }
}
